from pyquery import PyQuery as pq
import urllib.parse
import requests
from module import Function
import re
import os

class Parser(object):
    def paser(self, page_url, html_cont, basedir):
        doc = pq(html_cont)
        doc('.player-list').remove()
        doc('.video-info').remove()
        doc('.v_aside').remove()
        doc('#pageFooter').remove()
        doc('.breadcrumb').remove()
        doc('.navbar').remove()
        doc('img.logo').parent().attr('href','#')
        doc('.search').remove()
        doc('#cmplayer').css('width','100%').css('text-align','center').css('margin','0 auto').css('background','#fff')

        html_cont = doc.outer_html()
        pattern = re.compile(r"<[^\s]*?/>")
        match = pattern.findall(html_cont)
        if match:
            for m in match:
                html_cont = html_cont.replace(m,'')
        new_urls = set()
        links = doc('.content a').items()
        for link in links:
            new_url = link.attr.href
            if len(new_url) < 5:
                continue
            check = False
            if new_url.find('http') == -1 and new_url.find('.') > -1:
                check = True
            elif new_url.find('tv.81.cn') > -1:
                check = True
            if check == True:
                new_full_url = urllib.parse.urljoin(page_url, new_url)
                new_urls.add(new_full_url)

        pattern = re.compile(r'<!--mediaurl-->\s+(http:.*?\.mp4)')
        match = pattern.findall(html_cont)
        if match:
            for m in match:
                print(m)
                filename = os.path.basename(m)
                print('下载视频开始：'+filename)
                dir = Function.getsavedir(page_url,basedir)
                filepath = dir+'/'+filename
                if os.path.isfile(filepath)==False:
                    Function.downfile2(m,filepath)
                    html_cont = html_cont.replace(m,filename)
                    print('下载视频结束：' + filename)
                else:
                    print('下载视频已存在：' + filename)
        Function.savehtml(page_url,basedir,html_cont)
        return new_urls,html_cont
